// pages/common/register.js
var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })
    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }, 1000)
};
Page({
  data: {
    last_time: '',
    is_show: true,
    mesgCode: '',
    newTel: '',
    isGet: false,
    formType: 0, //从非结算页进入,2从会员专供结算页进入
    isDisRegister: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      formType: options.type == undefined ? 0 : options.type
    })
    console.log(options)
  },
  getCheckCode() {
    var that = this;
    // 将获取验证码按钮隐藏60s，60s后再次显示
    that.setData({
      isGet: true
    })
    if (that.data.newTel != '') {
      server.postJSON(common.basePath + 'member/getbindcode/', {
        "mobile": that.data.newTel,
        "type": 3
      }, res => {
        if (res.data.errno == "0") {
          common.showErrorToast(res.data.errmsg);
          this.setData({
            is_show: false,
            isGet: false
          })
          settime(that);
        } else {
          this.setData({
            is_show: true,
            isGet: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else {
      this.setData({
        is_show: true
      })
      common.showErrorToast('请输入手机号！');
    }
  },
  getMesgCode(e) {
    this.setData({
      mesgCode: e.detail.value
    })
  },
  getNewTel(e) {
    this.setData({
      newTel: e.detail.value.replace(/\s+/g, '')
    })
  },
  //手机号登录
  bindScore() {
    this.setData({
      isDisRegister: true
    })
    if (this.data.newTel == '') {
      common.showErrorToast('手机号不能为空！');
      this.setData({
        isDisRegister: false
      })
    } else if (this.data.mesgCode == '') {
      common.showErrorToast('请先获取验证码！');
      this.setData({
        isDisRegister: false
      })
    } else {
      server.postJSON(common.basePath + 'member/bind/', {
        "mobile": this.data.newTel,
        "code": this.data.mesgCode
      }, res => {
        if (res.data.errno == "0") {
          var data = res.data.data;
          common.showErrorToast('登录成功！');
          this.setData({
            isCheckCode: false,
            isDisRegister: false
          })
          app.globalData.isLogin = true;
          this.storageUserinfo(data);
          console.log(this.data.formType)
          if (this.data.formType == 2) {
            wx.getStorage({
              key: 'memberaccountlist',
              success: resp => {
                console.log(resp)
                resp.data.score_mobile = data.member.cellphone;
                wx.setStorage({
                  key: 'memberaccountlist',
                  data: resp.data,
                })
                wx.navigateBack();
              },
              fail: resp => {
                wx.navigateBack();
              }
            })
          } else {
            wx.navigateBack();
          }
        } else {
          this.setData({
            is_show: true,
            isDisRegister: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    }
  },
  //本地存储用户信息
  storageUserinfo(data) {
    wx.setStorage({
      key: "userInfo",
      data: data
    })
  },
})